/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2015-02-10 15:07:44
 * @filename : static/site/nff/controller/composeUser.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module) {
    require('{component}promptTip/promptTip.css');
    var ComposeView, nav='composeUser', scrollTop = 0, scrollAjax = true, 
        mId = 'composeUser',reRender = false,menuName = 'composeUser',
        condition = {
            page    : 0,
            from    : 0,
            offset  : 20
        },
        tpl = require('{view}composeUser.tpl'),
        promptTip = require('{component}promptTip/promptTip'),
        panel = require('{component}panel/zepto.panel'),
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        tplL = require('{view}composeUserLi.tpl');
        tplN = require('{view}userNav.tpl');
        
    ComposeUserView = Backbone.View.extend({
        
        el: '',
          
        template: _.template(tpl),
        templateN: _.template(tplN),
        templateL: _.template(tplL),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            share.reRender(reRender,mId);
            window.scroll(0,0);
            if ($('#'+mId).length < 1) { 
                $('body').append(this.template());
                this.bindEvt();            
            } else {
                $('#'+mId).css('display','block');
            }
           
            share.panel(panel,this.templateN,mId,menuName);

            var dom = $('#'+mId).find('.body_all').find('ul');
                if (dom.find('li').length < 1) {
                    this.getUserList(condition); 
                } else {
                    window.scroll(0,0);
                }
        },

        bindEvt : function() {
            var tthis = this,dom = $('#'+mId);    
 
            dom.find('.back').on('tap',function(e){
                e.stopPropagation();
                window.history.go(-1);
            }); 

            dom.find('.list').on('tap',function(e){
               var uid = $(e.target).closest('li').attr('uid'),
                   userName = $(e.target).closest('li').attr('username');
               window.location.href = '#compose/whole/custom'+JSON.stringify({type:'addUser',uid:uid,userName:userName});
            });

            $(window).on('scroll.'+mId,function(e){
                if(seajs.moduleUI == mId) {
                    var WH = $(window).height(),
                        SH = $(window).scrollTop(),
                        DH = $('#'+mId).height();

                    scrollTop = SH;    
                    if (SH > 0 && (DH-WH-SH) < 100) { 
                        tthis.getUserList(condition);
                    }
                    dom.find('.body_all').imglazyload({
                        startY : SH,
                        endY:SH + $(window).height()
                    });
                }
            });
        },
            
        getUserList : function() {
            var tthis = this,
                dom =  $('#'+mId).find('.body_all').find('ul');

            var obj = {
                //url : seajs.data.vars.apiAccessUrl+'my_visitors',
                url : seajs.data.vars.apiAccessUrl+'wm_get_contacts',
                type : 'POST',
                data : data,
                success : function(data) {
                    var Img = seajs.data.vars.resourceUrl+'img/';
                    if(data.contact.length < 1 && dom.find('li').length < 1) {
                        promptTip.show({type:'noData',text:'This will give you access to everyone you\'ve ever emailed.', dom:dom});
                        return; 
                    }
                    dom.append(tthis.templateL({arr:data.contact,defaultImg:Img,user:data.info}));
                    dom.imglazyload({startY : 0, endY: $(window).height()}); 

                },
                error : function(xhr, errorType, error) {},
                complete : function(xhr, status) {
                    share.ajaxEndB($('#'+mId).find('ul'));
                    setTimeout(function(){
                        scrollAjax = true;
                    },500)
                }
            };
            if (scrollAjax) {
                promptTip.hide({type:'noData', dom:dom});
                scrollAjax = false;
                share.ajaxStartB(dom);
                $.ajax(share.ajaxControl(obj));
            }
        }
    });
    module.exports = ComposeUserView;
});




